package top.canyue.word_range_service.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.canyue.word_range.exception.BaseException;
import top.canyue.word_range.pojo.dto.WordDto;
import top.canyue.word_range.pojo.po.Word;
import top.canyue.word_range.pojo.po.WordMapping;
import top.canyue.word_range.pojo.vo.WordDetailVo;
import top.canyue.word_range.result.Result;
import top.canyue.word_range.utils.PojoUtil;
import top.canyue.word_range_service.mapper.WordDetailMapper;
import top.canyue.word_range_service.mapper.WordMapper;
import top.canyue.word_range_service.mapper.WordMappingMapper;
import top.canyue.word_range_service.service.WordService;

@Service
@RequiredArgsConstructor
public class WordServiceImpl extends ServiceImpl<WordMapper,Word> implements WordService {
    final WordMapper wordMapper;
    final WordMappingMapper wordMappingMapper;
    final WordDetailMapper wordDetailMapper;

    @Transactional
    @Override
    public Result<Object> addWord(WordDto wordDto) {
        Word word = BeanUtil.copyProperties(wordDto, Word.class, PojoUtil.getNullPropertyNames(wordDto));
        // TODO: 重复的单词，保持原先的难度 OR 覆盖难度 ？
        wordMapper.insertIgnoreInto(word);
        WordMapping wordMapping = BeanUtil.copyProperties(wordDto, WordMapping.class, PojoUtil.getNullPropertyNames(wordDto));
        wordMappingMapper.insert(wordMapping);
        return Result.success();
    }

    @Override
    public Result<WordDetailVo> getWordDetail(String word, String bookId) {
        WordDetailVo wordDetailVo = wordDetailMapper.getWordDetail(word, bookId);
        return Result.success(wordDetailVo);
    }

    @Override
    @Transactional
    public Result<Object> updateWord(WordDto wordDto) {
        Word word = BeanUtil.copyProperties(wordDto, Word.class,PojoUtil.getNullPropertyNames(wordDto));
        LambdaUpdateWrapper<Word> wordUpdateWrapper = new LambdaUpdateWrapper<Word>();
        wordUpdateWrapper.eq(Word::getWord,word.getWord());
        boolean notOk = wordMapper.update(word, wordUpdateWrapper) != 1;
        if(notOk){
            throw new BaseException("更新单词基本信息失败");
        }
        WordMapping wordMapping = BeanUtil.copyProperties(wordDto, WordMapping.class, PojoUtil.getNullPropertyNames(wordDto));
        LambdaUpdateWrapper<WordMapping> mappingUpdateWrapper = new LambdaUpdateWrapper<WordMapping>();
        mappingUpdateWrapper.eq(WordMapping::getWord,word.getWord());
        mappingUpdateWrapper.eq(WordMapping::getBookId,wordMapping.getBookId());
        boolean notOk2 = wordMappingMapper.update(wordMapping,mappingUpdateWrapper) != 1;
        if(notOk2){
            throw new BaseException("更新单词意译信息失败");
        }
        return Result.success();
    }

    @Override
    public Result<Object> deleteWordOnBook(String word, String bookId) {

        return null;
    }

}
